Page({
  data: {
    show: false,
    chooseTime: false,
    avatarUrl: '',
    currentDate: +new Date(),
    minDate: new Date(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}`
      }
      if (type === 'month') {
        return `${value}`
      }
      return value
    },
  },
  async onLoad() {
    const { data } = await wx.http.get('/v1_0/user/profile')
    console.log(data)
    this.setData({
      ...data,
    })
    console.log(this.data)
  },
  //显示男女弹出层
  showPopup() {
    this.setData({ show: true })
  },
  onClose() {
    this.setData({ show: false })
  },
  changeMan() {
    this.setData({ gender: 1, show: false })
    this.updateMessage()
  },
  changeWoman() {
    this.setData({ gender: 0, show: false })
    this.updateMessage()
  },
  chooseBirthday() {
    this.setData({
      chooseTime: true,
    })
  },
  //时间选择
  onInput(event) {
    this.setData({
      currentDate: event.detail,
    })
    console.log(event.detail)
  },
  quxiaoTime() {
    this.setData({
      chooseTime: false,
    })
  },
  // 确定，更改时间
  async quedingTime() {
    const timestamp = this.data.currentDate
    const date_object = new Date(timestamp)

    // 获取年、月、日并拼接为字符串，用横杠分隔
    const year = date_object.getFullYear()
    const month = String(date_object.getMonth() + 1).padStart(2, '0')
    const day = String(date_object.getDate()).padStart(2, '0')
    const formatted_date = `${year}-${month}-${day}`
    this.setData({
      chooseTime: false,
      birthday: formatted_date,
    })
    this.updateMessage()
  },
  //更新用户信息
  async updateMessage() {
    const res = await wx.http({
      url: '/v1_0/user/profile',
      method: 'patch',
      parmas: {
        name: this.data.name,
        gender: this.data.gender,
        birthday: this.data.birthday,
        intro: this.data.intro,
      },
    })
    console.log(res)
  },

  getAvatarUrl({ detail }) {
    console.log(detail, 'sdfsd')
    this.setData({
      avatarUrl: detail.avatarUrl,
    })
    wx.uploadFile({
      url: wx.http.baseURL + '/v1_0/user/photo',
      name: 'photo',
      filePath: detail.avatarUrl,
      header: {
        Authorization: getApp().token,
      },
      success: ({ data }) => {
        console.log(data, '123123')
      },
    })
  },
  //去求改昵称
  changeName() {
    wx.navigateTo({
      url: '/pages/editName/editName?name=' + this.data.name,
    })
  },
  //去求改简介
  changeIntro() {
    wx.navigateTo({
      url: '/pages/editName/editName?intor' + this.data.intor,
    })
  },
})
